<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/28
 * Time: 11:05
 */

namespace common\logics\taskcommod;

use common\widgets\Tool;
use Yii;
use yii\base\BaseObject;

class TaskCommodLogic extends BaseObject implements ITaskCommod
{
    /** @var \common\sources\read\taskcommod\TaskCommodAccess $RTaskCommodAccess ***/
    private $RTaskCommodAccess;
    /** @var \common\sources\write\taskcommod\TaskCommodAccess $WTaskCommodAccess **/
    private $WTaskCommodAccess;
    /** @var \common\sources\read\valueservice\ValueServiceAccess $RValueServiceAccess **/
    private $RValueServiceAccess;
    /** @var \common\sources\write\valueservice\ValueServiceAccess $WValueServiceAccess **/
    private $WValueServiceAccess;
    /*** @var \common\sources\read\member\MemberAccess $RMemberAccess */
    private $RMemberAccess;
    /** @var \common\sources\read\shop\ShopAccess $RShopAccess */
    private $RShopAccess;
    /** @var \common\sources\write\shop\ShopAccess $WShopAccess **/
    private $WShopAccess;
    /** @var \common\sources\read\taskcommodkeywords\TaskCommodKeywordAccess $RTaskCommodKeywordAccess  */
    private $RTaskCommodKeywordAccess;
    /** @var \common\sources\write\taskcommodkeywords\TaskCommodKeywordAccess $WTaskCommodKeywordAccess  */
    private $WTaskCommodKeywordAccess;
    /** @var \common\sources\read\businesstaskcommodlog\BusinessTaskCommodLogAccess $RBusinessTaskCommodLogAccess */
    private $RBusinessTaskCommodLogAccess;
    /** @var \common\sources\write\businesstaskcommodlog\BusinessTaskCommodLogAccess $WBusinessTaskCommodLogAccess */
    private $WBusinessTaskCommodLogAccess;
    /** @var \common\sources\read\taskshopimage\TaskShopImageAccess $RTaskShopImageAccess */
    private $RTaskShopImageAccess;
    /** @var \common\sources\write\taskshopimage\TaskShopImageAccess $WTaskShopImageAccess */
    private $WTaskShopImageAccess;
    /** @var \common\sources\read\taskorder\TaskOrderAccess $RTaskOrderAccess ***/
    private $RTaskOrderAccess;
    /** @var \common\sources\read\praisekeywords\PraiseKeywordsAccess $RPraiseKeywordsAccess */
    private $RPraiseKeywordsAccess;
    /** @var \common\sources\write\praisekeywords\PraiseKeywordsAccess $WPraiseKeywordsAccess */
    private $WPraiseKeywordsAccess;
    /** @var \common\sources\read\adminuser\AdminUserAccess $RAdminUserAccess **/
    private $RAdminUserAccess;
    /** @var \common\sources\read\taobao\TaoBaoAccess $RTaoBaoAccess */
    private $RTaoBaoAccess;
    /** @var \common\sources\read\bank\BankAccess $RBankAccess **/
    private $RBankAccess;
    /** @var \common\sources\read\valueserviceprohibit\ValueServiceProhibitAccess  $RValueServiceProhibitAccess   **/
    private $RValueServiceProhibitAccess;
    private $commisse;
	/** @var \common\sources\write\businessacount\BusinessAcountAccess  $WBusinessAcountAccess   **/
	private $WBusinessAcountAccess;
	/** @var \common\sources\write\businessdepositrecord\BusinessDepositRecordAccess $WBusinessDepositRecordAccess */
	private $WBusinessDepositRecordAccess;
	/** @var \common\sources\write\businesspepperrecord\BusinessPepperRecordAccess  $WBusinessPepperRecordAccess */
	private $WBusinessPepperRecordAccess;
	/** @var \common\sources\read\businessaccount\BusinessAccountAccess $RBusinessAccountAccess   **/
	private $RBusinessAccountAccess;



    public function init()
    {
        $this->commisse = require dirname(dirname(dirname(dirname(__FILE__)))).'/config/value-service-config.php';
        $this->RTaskCommodAccess = Yii::$container->get("RTaskCommodAccess");
        $this->WTaskCommodAccess  = Yii::$container->get("WTaskCommodAccess");
        $this->RValueServiceAccess = Yii::$container->get("RValueServiceAccess");
        $this->WValueServiceAccess = Yii::$container->get("WValueServiceAccess");
        $this->RMemberAccess = Yii::$container->get("RMemberAccess");
        $this->WShopAccess = Yii::$container->get('WShopAccess');
        $this->RShopAccess = Yii::$container->get('RShopAccess');
        $this->RTaskCommodKeywordAccess = Yii::$container->get('RTaskCommodKeywordAccess');
        $this->WTaskCommodKeywordAccess = Yii::$container->get('WTaskCommodKeywordAccess');
        $this->RBusinessTaskCommodLogAccess = Yii::$container->get('RBusinessTaskCommodLogAccess');
        $this->WBusinessTaskCommodLogAccess = Yii::$container->get('WBusinessTaskCommodLogAccess');
        $this->RTaskShopImageAccess = Yii::$container->get('RTaskShopImageAccess');
        $this->WTaskShopImageAccess = Yii::$container->get('WTaskShopImageAccess');
        $this->RTaskOrderAccess = Yii::$container->get("RTaskOrderAccess");
        $this->RPraiseKeywordsAccess = Yii::$container->get("RPraiseKeywordsAccess");
        $this->WPraiseKeywordsAccess = Yii::$container->get("WPraiseKeywordsAccess");
        $this->RAdminUserAccess = Yii::$container->get('RAdminUserAccess');
        $this->RTaoBaoAccess = Yii::$container->get('RTaoBaoAccess');
        $this->RBankAccess = Yii::$container->get('RBankAccess');
        $this->RValueServiceProhibitAccess = Yii::$container->get("RValueServiceProhibitAccess");
        $this->WBusinessAcountAccess = Yii::$container->get("WBusinessAcountAccess");
        $this->WBusinessDepositRecordAccess = Yii::$container->get("WBusinessDepositRecordAccess");
        $this->WBusinessPepperRecordAccess = Yii::$container->get("WBusinessPepperRecordAccess");
	    $this->RBusinessAccountAccess = Yii::$container->get("RBusinessAccountAccess");

        parent::init(); // TODO: Change the autogenerated stub
    }

    public function getList($request)
    {
        $search = [
            'plat_type' => intval($request->post('plat_type',0)),     //平台类型 1.淘宝，2.天猫
            'task_status' => intval($request->post('task_status',0)), //任务状态 1.未支付,2.已支付,3.审核不通过,4.进行中, 5.已取消，6.已完成。
            'task_type' => $request->post('task_type',0),     //任务类型
            'prio_audit' => $request->post('prio_audit',0),   //是否快速审核
            'search_type' => $request->post('search_type',0), //搜索类型 1.任务编号  2.商家账号  3.商家店铺  4商家旺铺
            'search_text' => trim($request->post('search_text','')),//  搜索内容
            'task_start_time' => $request->post('task_start_time',''),//生成开始时间
            'task_end_time' => $request->post('task_end_time',''),    //生成任务结束时间
            'page_num' => intval($request->post('page_num',1)),               //默认页数
            'page_count' => intval($request->post('page_count',10)),          //每页默认显示10条
        ];
        if(!empty($search['search_type']) && empty($search['search_text']))
        {
            return Tool::returnJson([],401, "选择搜索类型，搜索内容不能为空");
        }
        //查询列表总数量
        $count = $this->RTaskCommodAccess->getCount($search);
        if($count<$search['page_count'])
        {
            $search['page_num'] = 1;
        }

        $page_num = intval($search['page_num']);
        if(!$page_num){
            $page_num = 1;
        }
        if(!$search['page_count']){
            $search['page_count'] = 10;
        }
        $list = [];
        if($count >0 ){
            $list = $this->RTaskCommodAccess->getList($search);
            foreach($list as &$item){
                $item['prio_audit'] = !empty($item['prio_audit']) ? "订单优先审核" : "";
                $item['task_status'] =  !empty($item['task_status']) ? Yii::$app->params['task_status_name'][$item['task_status']] : '';
                $item['pay_time'] = !empty($item['pay_time']) ? date('Y-m-d H:i:s',$item['pay_time']) : "";
            }
        }

        return Tool::returnJson([
            "total" => $count,
            "current_page" => $page_num,
            "list" => $list,
        ],200);
    }
    //任务详情
	public function getTaskDetail($request) {
		$search = [
			'task_commod_id' => intval($request->post('task_commod_id', 0)),
		];
		if (empty($search['task_commod_id'])) {
			return Tool::returnJson([], 401, "任务id不能为空");
		}
		$task_commod = $this->RTaskCommodAccess->getDetial($search['task_commod_id']);
		if (empty($task_commod)) {
			return Tool::returnJson([], 402, "任务不存在");
		}
		$task_commod['pay_time'] = !empty($task_commod['pay_time']) ? date('Y-m-d H:i:s', $task_commod['pay_time']) : "";
		//获取关键词信息
		$task_commod['commod_keywords'] = $this->RTaskCommodKeywordAccess->TaskCommodKeyword($task_commod['id']);
		//任务操作日志
		$task_commod['action_log'] = $this->RBusinessTaskCommodLogAccess->GetLog($task_commod['id']);
		if (!empty($task_commod['action_log'])) {
			foreach ($task_commod['action_log'] as &$log) {
				$log['create_time'] = empty($log['create_time']) ? '' : date("Y-m-d H:i:s", $log['create_time']);
			}
			unset($log);
		}
//        if (1 == $task_commod['is_task_image']) {//图文任务
//            //图文好评关键词
//            $task_commod['high_opinion_keywords'] = is_null($this->RTaskShopImageAccess->keyWorkers($task_commod['id'])) ? '' : $this->RTaskShopImageAccess->keyWorkers($task_commod['id']);
//        } else {//非图文好评关键词
//            $task_commod['high_opinion_keywords'] = is_null($this->RPraiseKeywordsAccess->keyWorkers($task_commod['id'])) ? '' : $this->RPraiseKeywordsAccess->keyWorkers($task_commod['id']);
//        }
		//
		if (($task_commod['plat_type'] == 1 || $task_commod['plat_type'] == 2) && $task_commod['task_type'] != 2) {
			//非图文好评关键词
			$task_commod['high_opinion_keywords'] = is_null($this->RPraiseKeywordsAccess->keyWorkers($task_commod['id'])) ? '' : $this->RPraiseKeywordsAccess->keyWorkers($task_commod['id']);
		}

		//获取增值服务 聊天
		$value_service = $this->RValueServiceAccess->getList($search['task_commod_id']);
		if ($value_service) {
			$value_service['return_type'] = $value_service['return_type'] == 1 ? '平台返款' : ($value_service['return_type'] == 2 ? '商家返款' : '');
			$value_service['pub_timing']  = !empty($value_service['pub_timing'])?date('Y-m-d H:i:s',$value_service['pub_timing']):'--';
			$value_service['end_timing']  = !empty($value_service['end_timing'])?date('Y-m-d H:i:s',$value_service['end_timing']):'--';;
			if (isset($value_service['chat_type']) && !empty($value_service['chat_type'])) {
				$value_service['chat_type'] = $this->commisse['chat_type'][$value_service['chat_type']];
			}
			if (isset($value_service['exten_period']) && !empty($value_service['exten_period'])) {
				$value_service['exten_period'] = $this->commisse['exten_period'][$value_service['exten_period']];
			}
			if (isset($value_service['buy_sex']) && !empty($value_service['buy_sex'])) {
				$value_service['buy_sex'] = $this->commisse['buy_sex'][$value_service['buy_sex']];
			}
			$value_service['up_rank']    = '排名';
			$value_service['prio_audit'] = $value_service['prio_audit'] > 0 ? $this->commisse['prio_audit'][$value_service['prio_audit']] : '不优先';
			//禁止地址
			$value_service['prohibit']           = $this->RValueServiceProhibitAccess->getValueServiceProhibit($value_service['id']);
			$value_service['expre_service_name'] = !empty($value_service['expre_service']) ? Yii::$app->params['expre_type'][$value_service['expre_service']] : "";
		}
		//判断是否是图文任务
		$task_shop_image = [];
		if (($task_commod['plat_type'] == 1 || $task_commod['plat_type'] == 2) && $task_commod['task_type'] == 2) {
			$task_shop_image = is_null($this->RTaskShopImageAccess->keyWorkers($task_commod['id'])) ? '' : $this->RTaskShopImageAccess->keyWorkers($task_commod['id']);
		}

		$arr = [
			'task_commod'     => $task_commod,
			"value_service"   => $value_service,
			"task_shop_image" => $task_shop_image,
		];
		return Tool::returnJson($arr, 200, "成功");
	}
    //获取发货地
    public function getCommodAddr(){
        $arr = [
            [
                ["label" => "北京"],
                ["label" => "上海"],
                ["label" => "广州"],
                ["label" => "深圳"],
                ["label" => "杭州"],
                ["label" => "海外"],
                ["label" => "江浙沪"],
                ["label" => "京津冀"],
                ["label" => "港澳台"],
            ],
            [
                ["label" => "长沙"],
                ["label" => "长春"],
                ["label" => "成都"],
                ["label" => "重庆"],
                ["label" => "大连"],
                ["label" => "东莞"],
                ["label" => "佛山"],
                ["label" => "福州"],
                ["label" => "贵阳"],
                ["label" => "合肥"],
                ["label" => "金华"],
                ["label" => "济南"],
                ["label" => "嘉兴"],
                ["label" => "昆明"],
                ["label" => "宁波"],
                ["label" => "南昌"],
                ["label" => "南京"],
                ["label" => "青岛"],
                ["label" => "泉州"],
                ["label" => "沈阳"],
                ["label" => "苏州"],
                ["label" => "天津"],
                ["label" => "温州"],
                ["label" => "无锡"],
                ["label" => "武汉"],
                ["label" => "西安"],
                ["label" => "厦门"],
                ["label" => "郑州"],
                ["label" => "中山"],
                ["label" => "石家庄"],
                ["label" => "哈尔滨"],
            ],
            [
                ["label" => "安徽"],
                ["label" => "福建"],
                ["label" => "甘肃"],
                ["label" => "广东"],
                ["label" => "广西"],
                ["label" => "贵州"],
                ["label" => "海南"],
                ["label" => "河北"],
                ["label" => "河南"],
                ["label" => "湖北"],
                ["label" => "湖南"],
                ["label" => "江苏"],
                ["label" => "江西"],
                ["label" => "吉林"],
                ["label" => "辽宁"],
                ["label" => "宁夏"],
                ["label" => "青海"],
                ["label" => "山东"],
                ["label" => "山西"],
                ["label" => "陕西"],
                ["label" => "云南"],
                ["label" => "四川"],
                ["label" => "西藏"],
                ["label" => "新疆"],
                ["label" => "浙江"],
                ["label" => "澳门"],
                ["label" => "香港"],
                ["label" => "台湾"],
                ["label" => "内蒙古"],
                ["label" => "黑龙江"],
            ]
        ];
        return Tool::returnJson($arr,200,"成功");
    }

    //任务审核
	public function AuditTask($data) {
		if (empty($data['id'])) {
			return Tool::returnJson([], 401, "订单id不能为空");
		}
		if (empty($data['status'])) {
			return Tool::returnJson([], 402, "状态不能为空");
		}
		if (empty($data['bus_id'])) {
			return Tool::returnJson([], 403, "商家id不能为空");
		}
		if (empty($data['operator'])) {
			return Tool::returnJson([], 405, "操作人不能为空");
		}
		if (empty($data['task_id'])) {
			return Tool::returnJson([], 406, "任务id不能为空");
		}
		if (3 == $data['status']) {//审核不通过前先判断是否有买手接单
			$accept = $this->RTaskOrderAccess->AcceptTaskSub($data['id']);
			if ($accept) {
				return Tool::returnJson([], 407, "该任务已经有买手接单");
			}
			if(empty($data['reason']))
			{
                return Tool::returnJson([],408,"审核不通过原因不能为空");
            }
		}

		$trans = Yii::$app->db->beginTransaction();
		try {
			if (2 == $data['status']) {//暂停
				$data['task_status']   = 2;
				$data['status_remark'] = '任务已暂停';

			}else if (3 == $data['status']) {//不通过

				$data['task_status']   = 3;
				$data['status_remark'] = '审核不通过';

				//资金返还操作
				/*===========================================================================*/
				//任务详情
				$task_commod = $this->RTaskCommodAccess->getTaskCommodInfo($data['id']);
				if ($task_commod['task_status'] !=2) {
					return Tool::returnJson([], 411, "该状态不可操作");
				}

				//商家资金
				$business_account = $this->RBusinessAccountAccess->getBusinessAccount($task_commod['bus_id']);

				$pay_task_amount   = $task_commod['pay_task_amount'];                 //任务押金金额
				$pay_task_commisse = $task_commod['pay_task_commisse'];              //任务佣金
				$ava_deposit       = number_format($business_account['ava_deposit'] + $pay_task_amount,2,".","");
				$ava_pepper        = number_format($business_account['ava_pepper'] + $pay_task_commisse,2,".","");
				$freezing_deposit  = number_format($business_account['freezing_deposit'] - $pay_task_amount,2,".","");
				$freezing_golds    = number_format($business_account['freezing_golds'] - $pay_task_commisse,2,".","");
				//修改账号余额
				$accout_arr = [
					"ava_deposit"      => $ava_deposit,
					"ava_pepper"       => $ava_pepper,
					"freezing_deposit" => $freezing_deposit,
					"freezing_golds"   => $freezing_golds,
					'bus_id'           => $task_commod['bus_id'],
				];
				$account_res = $this->WBusinessAcountAccess->editAcount($accout_arr);
				if ($account_res === false) {
					$trans->rollBack();
					return Tool::returnJson([], 411, "返还商家押金和花椒失败");
				}

				//退押金记录
				$deposit_arr  = [
					"bus_id"         => $task_commod['bus_id'],
					"task_commod_id" => $task_commod['id'],
					"task_id"        => $task_commod['task_id'],
					"expend"         => 0,
					"type"           => 3,
					"income"         => $pay_task_amount,
					"balance"        => $ava_deposit,
                    "freeze_change" => -$pay_task_amount,
                    "freeze_balance" => number_format($business_account['freezing_deposit'] - $pay_task_amount,2,".",""),
					"remark"         => "审核不通过退还押金",
					"operatorId"     => $data['operator'],
				];

				$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposit_arr);
				if (!$deposite_res) {
					$trans->rollBack();
					return Tool::returnJson([], 412, "审核不通过退还押金失败");
				}
				//退花椒
				$pepper_arr = [
					"bus_id"         => $task_commod['bus_id'],
					"task_commod_id" => $task_commod['id'],
					"task_id"        => $task_commod['task_id'],
					"expend"         => 0,
					"type"           => 3,
					"income"         => $pay_task_commisse,
					"balance"        => $ava_pepper,
                    "freeze_change" =>  -$pay_task_commisse,
                    "freeze_balance" => number_format($business_account['freezing_golds'] - $pay_task_commisse,2,".",""),
					"remark"         => "审核不通过退还花椒",
					"operatorId"     => $data['operator']
				];
				$pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($pepper_arr);
				if (!$pepper_res) {
					$trans->rollBack();
					return Tool::returnJson([], 413, "审核不通过退还花椒失败");
				}

				/*===========================================================================*/
			}else if (4 == $data['status']) {//通过
				$data['task_status']   = 4;
				$data['status_remark'] = '进行中';
			}
            $this->WTaskCommodAccess->AuditTask($data);//修改状态
			$this->WBusinessTaskCommodLogAccess->AddTaskLog($data);
			$trans->commit();
			return Tool::returnJson([], 200, "成功");
		} catch (\Exception $e) {
			$trans->rollBack();
			return Tool::returnJson([], 409, "审核失败" . $e->getMessage());
		}
	}

    public function IsPrint($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"订单id不能为空");
        }
        if (empty($data['is_print'])) {
            return Tool::returnJson([],401,"是否打印");
        }
        $result = $this->WTaskCommodAccess->IsPrint($data);
        if ($result) {
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],409,"失败");
    }

    public function ModifyNotic($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"订单id不能为空");
        }
        $result = $this->WTaskCommodAccess->ModifyNotic($data);
        if ($result) {
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],409,"失败");
    }

    public function ModifyBranchType($data) {
        if (empty($data['shopId'])) {
            return Tool::returnJson([],401,"店铺id不能为空");
        }
        if (empty($data['mainType'])) {
            return Tool::returnJson([],401,"主类目不能为空");
        }
        if (empty($data['mainCode'])) {
            return Tool::returnJson([],401,"主类目id不能为空");
        }
        if (empty($data['subType'])) {
            return Tool::returnJson([],401,"子类目不能为空");
        }
        if (empty($data['subCode'])) {
            return Tool::returnJson([],401,"子类目id不能为空");
        }
        $result = $this->WShopAccess->ModifyBranchType($data);
        if ($result) {
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],409,"失败");
    }

    public function ModifySpu($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"任务id不能为空");
        }
        if (empty($data['commod_link'])) {
            return Tool::returnJson([],401,"商品链接不能为空");
        }
        if (empty($data['title'])) {
            return Tool::returnJson([],401,"商品标题不能为空");
        }
        if (empty($data['main_url1'])) {
            return Tool::returnJson([],401,"商品主图不能为空");
        }
        if (empty($data['colour']) || empty($data['size'])) {
            return Tool::returnJson([],401,"规格不能为空");
        }
        $result = $this->WTaskCommodAccess->ModifySpu($data);
        if ($result) {
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],409,"失败");
    }

    public function AddTaskRemark($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"id不能为空");
        }
        $result = $this->WBusinessTaskCommodLogAccess->AddTaskRemark($data);
        if ($result) {
            return Tool::returnJson([],200,'备注成功');
        }
        return Tool::returnJson([],409,'备注失败');
    }

    public function ModifyChatKeyWords($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"id不能为空");
        }
        $result = $this->WValueServiceAccess->ModifyChatKeyWords($data);
        if ($result) {
            return Tool::returnJson([],200,'成功');
        }
        return Tool::returnJson([],409,'失败');
    }

    public function ModifyCommentKeyWords($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"id不能为空");
        }
        if (empty($data['task_id'])) {
            return Tool::returnJson([],401,"task_id不能为空");
        }
        if (empty($data['praise'])) {
            return Tool::returnJson([],401,"类型不能为空");
        }
        if (empty($data['comment'])) {
            return Tool::returnJson([],401,"内容不能为空");
        }
        if (1 == $data['praise']) {
            return Tool::returnJson([],401,"默认好评不支持修改");
        }

	    $result = false;
        if (1 == $data['is_task_image']) {//是图文任务
            if (2 == $data['praise']) {//优质好评
                $result = $this->WTaskShopImageAccess->ModifyCommentKeyWords($data);
            }
            if (3 == $data['praise']) {//自定义好评
                if (is_array($data['comment'])) {
                    foreach ($data['comment'] as $key => $value) {//依次更新
                        $result = $this->WTaskShopImageAccess->ModifySelfKeyWords($value['id'],$value['word']);
                    }
                }
            }
        }
        if (0 == $data['is_task_image']) {//非图文任务

            if (2 == $data['praise']) {//优质好评
                $result = $this->WValueServiceAccess->editCommentKeyWords($data);
            }

            if (3 == $data['praise']) {//自定义好评

                if (is_array($data['comment'])) {
                    foreach ($data['comment'] as  $_item) {//依次更新
                        $result = $this->WPraiseKeywordsAccess->editSelfKeyWords($_item['id'],$_item['word']);
                    }
                }
            }
        }
        if ($result !== false) {
            return Tool::returnJson([],200,'修改成功');
        }
        return Tool::returnJson([],409,'修改失败');
    }

    public function ModifyTaskKeyWords($data) {
        if (empty($data['id'])) {
            return Tool::returnJson([],401,"id不能为空");
        }
        if (empty($data['task_id'])) {
            return Tool::returnJson([],401,"id不能为空");
        }
        if (empty($data['keywords'])) {
            return Tool::returnJson([],401,"关键词不能为空");
        }
        if (empty($data['ship_addr'])) {
            return Tool::returnJson([],401,"地址不能为空");
        }
        if (empty($data['range_price'])) {
            return Tool::returnJson([],401,"价格区间不能为空");
        }
        $trans = Yii::$app->db->beginTransaction();
        try {
            //修改价格区间，地址
            $result = $this->WTaskCommodAccess->ModifyTaskKeyWords($data);
            //修改关键字
            $result2 = $this->WTaskCommodKeywordAccess->ModifyTaskKeyWords($data);
            $trans->commit();
            return Tool::returnJson([],200," 成功");
        } catch (\Exception $e) {
            $trans->rollBack();
        }
        return Tool::returnJson([],409," 失败");
    }

	public function AuditData($data) {
		//先查询出后台所有工作人员
		$worker     = $this->RAdminUserAccess->getList();
		$audit_data = [];
		foreach ($worker as $key => $value) {
			$data['worker']               = $value['phone'];//工作人员手机
			$audit_data[$key]['phone']    = $value['phone'];
			$audit_data[$key]['username'] = $value['username'];
			//审核任务数据统计数量
			$audit_data[$key]['auditCommodPass']  = $this->RTaskCommodAccess->AuditDataPass($data);
			$audit_data[$key]['auditCommodRefus'] = $this->RTaskCommodAccess->AuditDataRefus($data);
			//审核买号统计
			$audit_data[$key]['auditTaoBaoPass']  = $this->RTaoBaoAccess->AuditDataPass($data);
			$audit_data[$key]['auditTaoBaoRefus'] = $this->RTaoBaoAccess->AuditDataRefus($data);
			//审核订单统计
			$audit_data[$key]['auditOrderPass']  = $this->RTaskOrderAccess->AuditDataPass($data);
			$audit_data[$key]['auditOrderRefus'] = $this->RTaskOrderAccess->AuditDataRefus($data);
			//审核会员提现账户统计
			$audit_data[$key]['auditBankPass']  = $this->RBankAccess->AuditDataPass($data);
			$audit_data[$key]['auditBankRefus'] = $this->RBankAccess->AuditDataRefus($data);
			//审核商家店铺统计
			$audit_data[$key]['auditShopPass']  = $this->RShopAccess->AuditDataPass($data);
			$audit_data[$key]['auditShopRefus'] = $this->RShopAccess->AuditDataRefus($data);
		}
		return Tool::returnJson($audit_data, 200, '成功');
	}


	/**
	 * getTaskPrice 获取任务价格详情
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/21
	 * @return array
	 */
	public function getTaskPrice($data) {
		if (empty($data['task_id'])) {
			return Tool::returnJson([], 401, "任务编号不能为空");
		}
		$info = $this->RTaskCommodAccess->getTaskDetial($data['task_id']);
		if (empty($info)) {
			return Tool::returnJson([], 401, "任务编号不存在！");
		}
		if (!in_array($info['task_status'], [2, 4])) {
			return Tool::returnJson([], 401, "任务状态不可操作！");
		}
		return Tool::returnJson($info, 200, '成功');
	}

	/**
	 * getValueServiceType 获取增值服务类型
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/22
	 * @return array
	 */
	public function getValueServiceType() {
		$info = $this->RValueServiceAccess->getType();
		return Tool::returnJson($info, 200, '成功');
	}

	/**
	 * valueServiceStatistics 增值服务统计
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/22
	 * @return false|string
	 */
	public function valueServiceStatistics($data) {
		if (empty($data['type'])) {
			return Tool::returnJson([], 401, "类型不能为空！");
		}
		$info      = [];
		$infoCount = $this->RValueServiceAccess->getValueServiceSListCount($data);

		//强制分页处理
		if ($infoCount > 0) {
			$tatol_page = ceil($infoCount / $data['limit']);
			if ($tatol_page < $data['page_num']) {
				$data['page_num'] = 1;
			}
		}

		if ($infoCount > 0) {
			$info['list'] = $this->RValueServiceAccess->getValueServiceSList($data);
			foreach ($info['list'] as &$val) {
				$taskCommodInfo           = $this->RTaskCommodAccess->getTaskDetial($val['task_id']);
				$val['task_num']          = 0;  // 任务总数
				$val['unanswered']        = 0;  // 未接手
				$val['have_in_hand']      = 0;  // 进行中
				$val['completed']         = 0;  // 已完成
				$val['fail_task_num']     = 0;  // 失效数
				$val['incomplete_number'] = 0;  // 公司收入
				if ($taskCommodInfo) {
					$val['task_num']         = $taskCommodInfo['task_num'];
					$val['unanswered']       = $taskCommodInfo['surplus_task_num'];  // 未接手
					$val['have_in_hand']     = $this->RTaskOrderAccess->getBuyOrderCounts('task_id = "' . $val['task_id'] . '"  and order_status in (1,2,3,4,5,6,7)');  // 进行中
					$val['completed']        = $this->RTaskOrderAccess->getBuyOrderCounts('task_id = "' . $val['task_id'] . '"  and order_status in ( 8,12,13)');  // 完成

					if ($taskCommodInfo['task_status'] == 5) {
						$val['fail_task_num'] = $taskCommodInfo['surplus_task_num'];
					}
				}
			}
			unset($val);
		}

		//返回分页数据
		$info['count']    = $infoCount;
		$info['page_num'] = $data['page_num'];
		$info['limit']    = $data['limit'];

		return Tool::returnJson($info, 200, '成功');
	}
}